package condominio.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import condominio.excepcion.DAOExcepcion;
import condominio.modelo.Reserva;
import condominio.modelo.Residente;
import condominio.modelo.EspaciosComunes;
import condominio.util.ConexionBD;


//Insertar 
public class ReservaDAO extends BaseDAO {

	public Reserva insertar(Reserva vo) throws DAOExcepcion {
		System.out.println("ReservaDAO: insertar()");
		String query = "INSERT INTO Reserva(FechaRESERVA,HoraInicio,HoraFin,DetalleRESERVA) VALUES (?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			//stmt.setInt(1, vo.getIdResidente());
			//stmt.setInt(1, vo.getIdReserva());
			
			stmt.setString(1, vo.getFechaReserva());
			stmt.setString(2, vo.getHoraInicio());
			stmt.setString(3, vo.getHoraFin());
			stmt.setString(4, vo.getDetalleReserva());
			//stmt.setInt(3, vo.getResidente().getIdResidente());
			//stmt.setInt(4, vo.getEspaciosComunes().getIdEspaciosComunes());
			
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public void eliminar(int idReserva) throws DAOExcepcion {
		String query = "DELETE FROM reserva WHERE idRESERVA=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idReserva);
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}
	public Reserva actualizar(Reserva vo) throws DAOExcepcion {
		System.out.println("ReservaDAO: actualizar()");
		String query = "update reserva set fechaRESERVA=?,HoraInicio=?,HoraFin=?,DetalleRESERVA=? where idRESERVA=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			
			stmt.setString(1, vo.getFechaReserva());
			stmt.setString(2, vo.getHoraInicio());
			stmt.setString(3, vo.getHoraFin());
			//stmt.setInt(4, vo.getEspaciosComunes().getIdEspaciosComunes());
			//stmt.setInt(5, vo.getResidente().getIdResidente());
			stmt.setInt(6, vo.getIdReserva());
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
}
